package com.isoft.service.impl;

import com.isoft.entity.DfmeLbdeEntity;
import com.isoft.service.DfmeLbdeService;
import com.isoft.service.XmlFileHandler;
import com.isoft.util.XmlParserUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

@Service
public class DfmeLbdeFileHandler implements XmlFileHandler {
    private static final Logger logger = LoggerFactory.getLogger(DfmeLbdeFileHandler.class);

    @Resource
    private DfmeLbdeService dfmeLbdeService;
    @Resource
    private XmlParserUtil xmlParserUtil;

    @Override
    public boolean supports(Path filePath) {
        // 支持文件名包含 LBDE 的 XML 文件
        String name = filePath.getFileName().toString().toUpperCase();
        return name.contains("LBDE") && name.endsWith(".XML");
    }

    @Override
    public String handle(Path filePath) throws Exception {
        // 1. 读取整个 XML 文件内容
        String xmlContent = Files.readString(filePath, StandardCharsets.UTF_8);
        // 2. 用 JAXB 解析为实体
        DfmeLbdeEntity entity = xmlParserUtil.parseXml(xmlContent, DfmeLbdeEntity.class);
        // 3. 调用服务保存
        dfmeLbdeService.parseAndSave(entity);
        logger.info("LBDE XML 已成功入库: {}", entity);
        return "LBDE XML 已成功入库";
    }
} 